/*
 * @Author: xiaosihan
 * @Date: 2024-01-20 10:22:49
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-01-20 10:22:49
 */

import ServiceBase from './ServiceBase';
import os from 'os';
import numeral from 'numeral';
import express from 'express';

export default class SystemService extends ServiceBase {

    constructor(req: express.Request = {} as express.Request, res: express.Response = {} as express.Response) {
        super(req, res);
    }

    // 系统状态检查接口
    ping = async (params: Record<string, any>) => {
        // 参数校验
        this.verifyParams([]);

        return {
            buildTime: process.env.BUILD_TIME,
            cpu: os.cpus().length, // 获取CPU信息  
            totalmem: numeral(os.totalmem()).format('0.0b'), // 总内存
            freemem: numeral(os.freemem()).format('0.0b'),// 空闲内存
        };
    };

    // 健康检查接口
    health_check = async (params: Record<string, any>) => {
        // 参数校验
        this.verifyParams([]);

        return {
            status: 'ok',
            message: '系统运行正常',
            timestamp: new Date().toISOString(),
            version: process.env.BUILD_TIME || 'unknown'
        };
    };
}